#include "srcreatewallet.h"
#include <QRegExpValidator>
#include <QSettings>
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QPushButton>
#include <QLineEdit>
#include <QLabel>
#include <QDir>

#include <QDebug>
#include <OEMessageBox>
#include "srdefine.h"
SRCreateWallet::SRCreateWallet(int _mode, QWidget *parent) : QWidget(parent)
{
    setWindowTitle(tr("用户登录"));
    QWidget* mainWidget = new QWidget();

//    QWidget* wdgt_logo = new QWidget();
//    QLabel* lab_logo = new QLabel();
    QLabel* lab_schedule = new QLabel();
//    lab_logo->setAlignment(Qt::AlignCenter);
//    lab_logo->setPixmap(QPixmap(":/lab/logo"));
//    QLabel* lab_logo_title = new QLabel("设置钱包名称");
//    lab_logo_title->setAlignment(Qt::AlignCenter);
//    lab_logo_title->setObjectName(QString("LogoTitle"));

    editUser_ = new QLineEdit();
    QLabel* labUser_ = new QLabel(tr("钱包名称:"));

//    editPswd_ = new QLineEdit();
//    QLabel* labPswd_ = new QLabel(tr("登录密码"));

    btnCreate_ = new QPushButton(tr("下一步"));

    if (_mode == 0)
        lab_schedule->setPixmap(QPixmap(":/register/nvgn1"));
    else {
        lab_schedule->setPixmap(QPixmap(":/register/nvgn_1"));
    }

    QVBoxLayout* layout = new QVBoxLayout();
    QHBoxLayout* layout_user = new QHBoxLayout();
    QHBoxLayout* layout_pswd = new QHBoxLayout();
//    QHBoxLayout* layout_logo = new QHBoxLayout();



    // 输入限制规则
    QRegExp regQuoteName("^[\u4e00-\u9fa50-9a-zA-Z]{0,16}$");
    QRegExpValidator* quoteValidator_name = new QRegExpValidator(regQuoteName, this);

    // 属性设置
    editUser_->setPlaceholderText(tr("可以用中文,英文,数字组合"));

    // 设置输入限制
    editUser_->setValidator(quoteValidator_name);

    // 规格
//    wdgt_logo->setFixedSize(250,80);
    editUser_->setFixedSize(160,30);
//    editPswd_->setFixedSize(160,30);
    btnCreate_->setFixedSize(100,40);
    mainWidget->setFixedSize(600,400);


    // 布局
//    layout_logo->addWidget(lab_logo);
//    layout_logo->addWidget(lab_logo_title);
//    wdgt_logo->setLayout(layout_logo);
    layout_user->addStretch();
    layout_user->addWidget(labUser_);
    layout_user->addSpacing(20);
    layout_user->addWidget(editUser_);
    layout_user->addStretch();
//    layout_pswd->addWidget(labPswd_);
//    layout_pswd->addWidget(editPswd_);
//    layout->addWidget(wdgt_logo,0,Qt::AlignLeft | Qt::AlignTop);
    layout->addWidget(lab_schedule, 0, Qt::AlignHCenter| Qt::AlignTop);
    layout->addStretch();
    layout->addLayout(layout_user);
    layout->addSpacing(70);
    layout->addWidget(btnCreate_,0,Qt::AlignHCenter);
    layout->addSpacing(30);

    mainWidget->setLayout(layout);

    // @marker: 因为登录界面比较简单，没有外窗口
    //          所以这边模拟了一个外窗口
    QGridLayout* temp_layout = new QGridLayout();
    temp_layout->addWidget(mainWidget);
    setLayout(temp_layout);


    // 信号链接
    connect(btnCreate_,SIGNAL(clicked()),
            this,SLOT(onCreate()));
}

void SRCreateWallet::onCreate() {
    // mkdir path
    if (editUser_->text().isEmpty()) {
        OEMessageBox::BriefMessage(this, tr("钱包名不能为空"));
        return;
    }
    QDir dir(editUser_->text());
    if(!dir.exists()) {
        dir.mkpath(editUser_->text());
    }
    else {
        OEMessageBox::BriefMessage(this, tr("该钱包已经存在"));
        return;
    }
    // move to path
//    QDir::setCurrent(editUser_->text());
    QString wallet_config_path = editUser_->text() + "/wallet";
    QSettings setting(wallet_config_path,QSettings::IniFormat);

    setting.setValue("INFO/name",editUser_->text());

    // res
    setting.setValue("RES/logo", "res/logo.png");
    setting.setValue("CUSTOM/seedname","audit.sr.seed");
    dir.mkdir("res");

    QPixmap pix(":/lab/logo");
    pix.save("res/logo.png");

    // 密语窗口


    emit sigFinshed(editUser_->text());
}
